﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace VIT.Provider
{
    public class Page : VIT.DataHelper.DataAccess.SQLSupport
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        #region Entities
        public string Id { get; set; }
        public string LanguageId { get; set; }
        public string Image { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Contents { get; set; }
        public string Tag { get; set; }
        public int Orders { get; set; }
        #endregion

        #region Constructor
        public Page(string id = "",
                        string languageId = "",
                        string image = "",
                        string title = "", 
                        string description = "",                        
                        string contents = "",
                        string tag = "",
                        int orders = 0)
        {
            this.Id = id;            
            this.LanguageId = languageId;
            this.Image = image;
            this.Title = title;
            this.Description = description;            
            this.Contents = contents;
            this.Tag = tag;
            this.Orders = orders;
        }
        #endregion

        #region Methods

        public DataTable GetList(string SortColumn = "", bool isTransaction = false)
        {
            if (SortColumn == "") SortColumn = "Orders";
            string strQuery = "select Id, Title, Description, Tag from Page where LanguageId=@LanguageId Order by " + SortColumn;
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            return ExecuteDataset(isTransaction, strQuery, LanguageId).Tables[0];
        }

        public void GetSingle(bool isTransaction = false)
        {
            string strQuery = "select * from Page where LanguageId=@LanguageId and Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            DataTable dt = ExecuteDataset(isTransaction, strQuery, Id, LanguageId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                this.Image = dt.Rows[0]["Image"].ToString();
                this.Title = dt.Rows[0]["Title"].ToString();
                this.Description = dt.Rows[0]["Description"].ToString();
                this.Contents = dt.Rows[0]["Contents"].ToString();
                this.Tag = dt.Rows[0]["Tag"].ToString();
                if (!string.IsNullOrEmpty(dt.Rows[0]["Orders"].ToString()))
                    this.Orders = int.Parse(dt.Rows[0]["Orders"].ToString());
            }
            else this.Id = "";
        }

        public bool Exist(string id, string languageid, bool isTransaction = false)
        {
            string strQuery = "select Id from Page where Id=@Id and LanguageID=@LanguageId";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", languageid);
            return ExecuteScalar(isTransaction, strQuery, Id, LanguageId) != null;
        }

        public bool Save()
        {
            bool isTransaction = false;
            if (Exist(this.Id, this.LanguageId, isTransaction))
                return Update(isTransaction);
            else
                return Insert(isTransaction);
        }

        public bool Insert(bool isTransaction = false)
        {
            string strQuery = "insert into Page (Id, LanguageId, Image, Title, Description, Contents, Tag, Orders, CreateDate, ModifyDate, CreateByUser, ModifyByUser) values (@Id, @LanguageId, @Image, @Title, @Description, @Contents, @Tag,@Orders, @Now, @Now, @User, @User)";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter Title = new SqlParameter("@Title", this.Title);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter Contents = new SqlParameter("@Contents", this.Contents);
            SqlParameter Tag = new SqlParameter("@Tag", this.Tag);
            SqlParameter Image = new SqlParameter("@Image", this.Image);
            SqlParameter Orders = new SqlParameter("@Orders", this.Orders);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);
            return ExecuteNonQuery(isTransaction, strQuery, Id, LanguageId, Image, Title, Description, Contents, Tag, Orders, Now, User) > 0;
        }

        public bool Update(bool isTransaction = false)
        {
            string strQuery = "update Page set Image=@Image, Title=@Title, Description=@Description, Contents=@Contents, Tag=@Tag, Orders=@Orders, ModifyDate=@Now, ModifyByUser=@User where Id=@Id and LanguageId=@LanguageId";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter Image = new SqlParameter("@Image", this.Image);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter Title = new SqlParameter("@Title", this.Title);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter Contents = new SqlParameter("@Contents", this.Contents);
            SqlParameter Tag = new SqlParameter("@Tag", this.Tag);
            SqlParameter Orders = new SqlParameter("@Orders", this.Orders);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, strQuery, Id, Image, LanguageId, Title, Description, Contents, Tag, Orders, Now, User) > 0;
        }

        public bool Delete(bool isTransaction = false)
        {
            string strQuery = "Delete from Page where Id=@Id and LanguageId=LanguageId";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            return ExecuteNonQuery(isTransaction, strQuery, Id, LanguageId) > 0;
        }
        #endregion       
    }
}
